﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.EntityClient;

namespace QLBH.ConfigLib
{
    public class EntityDBConnectionHelper
    {
        private QLBHSettings setting;
        public EntityDBConnectionHelper()
        {
            
        }
        private static EntityDBConnectionHelper instance;
        public static EntityDBConnectionHelper Instance
        {
            get
            {
                if (instance==null)
                {
                    instance = new EntityDBConnectionHelper();                    
                }
                return instance;
            }
        }
        public String GetEntityConnection()
        {
            setting = QLBHSettings.Instance;
            SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
            sqlBuilder.DataSource = setting.ServerName;
            sqlBuilder.InitialCatalog = "QLBH";
            if (setting.Authen)
            {
                sqlBuilder.IntegratedSecurity = true;
                sqlBuilder.MultipleActiveResultSets = true;
            }
            else
            {
                sqlBuilder.UserID = setting.UserName;
                sqlBuilder.Password = setting.PassWord;            
            }
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
            entityBuilder.Provider = "System.Data.SqlClient";
            entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
            entityBuilder.Metadata = @"res://*/QLBHModel.csdl|res://*/QLBHModel.ssdl|res://*/QLBHModel.msl";

            EntityConnection conn = new EntityConnection(entityBuilder.ToString());
            return conn.ConnectionString;
        }
        public String GetMasterConnectionString(bool authen,String serverName, String userName, String pass)
        {
            if (authen)
            {
                return String.Format("Server={0};Database=master;Trusted_Connection=Yes;", serverName);
            }
            else
            {
                return String.Format("Data Source= {0} ;User ID= {1};Password= {2}; Initial Catalog=master", serverName, userName, pass);
            }
            
        }

    }
}
